//
//  BHHomeCityLocationView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/6/9.
//

import UIKit

class BHHomeCityLocationView: UIView {

    @IBOutlet weak var cityBtn: UIButton!
    // 两个字是75
    @IBOutlet weak var cityBtnWidth: NSLayoutConstraint!
    
    @IBOutlet weak var refresh_locationBtn: UIButton!
    
    static func loadFromXib() -> BHHomeCityLocationView {
        let viewArray = Bundle.main.loadNibNamed("BHHomeCityLocationView", owner: nil, options: nil)
        let view: BHHomeCityLocationView = viewArray![0] as! BHHomeCityLocationView
        return view
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.cityBtn.addLayerCorner(4, 0.5, .color_34CE98)
    }
}



protocol BHHomeHotCityViewDelegate {
    func didHotCityChose(model: BHCommonDicModel)
}

class BHHomeHotCityView: UIView {
    
    @IBOutlet var hotCityItemArray: [UIButton]!
    var delegate: BHHomeHotCityViewDelegate?
    
    
    static func loadFromXib() -> BHHomeHotCityView {
        let viewArray = Bundle.main.loadNibNamed("BHHomeCityLocationView", owner: nil, options: nil)
        let view: BHHomeHotCityView = viewArray![1] as! BHHomeHotCityView
        return view
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        for item in self.hotCityItemArray {
            item.addLayerCorner(4, 0.5, .color_cccccc)
        }
    }
    
    var hotArray: [BHCommonDicModel] = [] {
        didSet {
            for (index, btn) in self.hotCityItemArray.enumerated() {
                if index < hotArray.count {
                    btn.isHidden = false
                    
                    let model: BHCommonDicModel = hotArray[index]
                    btn.setTitle(model.text, for: .normal)
                    
                }else {
                    btn.isHidden = true
                }
            }
        }
    }
    
    
    
    @IBAction func cityChoseAct(_ sender: UIButton) {
        let tag = sender.tag-100
        let model: BHCommonDicModel = hotArray[tag]
        self.delegate?.didHotCityChose(model: model)
    }
    
   static func hotHeight(count: Int) -> CGFloat {
        if count > 4 {
            return 150
        }else if count > 0 {
            return 100
        }else {
            return 50
        }
    }
    
}
